const { ccclass, property } = cc._decorator;

@ccclass
export default class Tile extends cc.Component {

    @property(cc.SpriteAtlas)
    blockSp: cc.SpriteAtlas = null;

    @property(cc.Node)
    blockBg: cc.Node = null;



    game = null;

    type: number = null;
    x: number = null;
    y: number = null;

    onLoad() {
        this.node.on(cc.Node.EventType.TOUCH_START, (event) => {
            this.check_click();
        }, this.node);

    }
    check_click() {
        this.game.operateLogic(this.x, this.y, this.type, true)
    }
    // 移动到特定点
    moveTo(x, y, isDown = true) {
        this.x = x;
        this.y = y;
        this.node.stopActionByTag(1);
        if (isDown) {
            var action = cc.spawn(
                cc.scaleTo(0.05, 0.95),
                cc.jumpTo(0.3, cc.p(this.node.width * x + this.node.width / 2, this.node.height * y + this.node.height / 2), 50, 1),
                cc.scaleTo(0.05, 1),
            );
        } else {
            var action = cc.moveTo(0.3, cc.p(this.node.width * x + this.node.width / 2, this.node.height * y + this.node.height / 2));
        }

        this.node.runAction(action);
        action.setTag(1);
    }
    // 产生新方块
    newTile(x, y) {
        this.node.setPosition(this.node.width * x + this.node.width / 2, cc.winSize.height);
        this.node.runAction(cc.jumpTo(0.3, cc.p(this.node.width * x + this.node.width / 2, this.node.height * y + this.node.height / 2), 50, 1));
        this.setArrPosition(x, y);
    }
    // 方块销毁
    destoryTile(fx) {
        var action = cc.sequence(
            cc.scaleTo(0.1, 0),
            cc.callFunc(function (node) {
                node.destroy();
            }, this.node, this.node));
        this.node.runAction(action);
        this.scheduleOnce(() => {
            this.game.fxBg.removeChild(fx)
        }, 2)
    }
    destoryTileMoveToTarItem(fx, tarItem: cc.Node) {
        var tempPlayer = this.node.parent.convertToNodeSpace(tarItem.convertToWorldSpace(tarItem.getAnchorPoint()))
        var self = this;
        var action = cc.sequence(
            cc.spawn(
                cc.moveTo(0.5, cc.p(tempPlayer.x + tarItem.width / 2, tempPlayer.y + tarItem.height / 2)),
                cc.scaleTo(0.5, 0.5),
            ),
            cc.callFunc(function (node) {
                tarItem.getComponent("TarItem").addTile()
                node.destroy();
                self.game.checkPass()
            }, this.node, this.node));
        this.node.setSiblingIndex(99999);
        this.node.runAction(action);
        this.scheduleOnce(() => {
            this.game.fxBg.removeChild(fx)
        }, 2)
    }
    // 设置方块在数组的位置
    setArrPosition(x, y) {
        this.x = x;
        this.y = y;
    }
    set_type(type, playEffect = true) {
        this.type = type;
        switch (type) {
            case 1:
                this.blockBg.getComponent(cc.Sprite).spriteFrame = this.blockSp.getSpriteFrame('block_red')
                this.node.addComponent(cc.ParticleSystem);
                break;
            case 2:
                this.blockBg.getComponent(cc.Sprite).spriteFrame = this.blockSp.getSpriteFrame('block_green')
                break;
            case 3:
                this.blockBg.getComponent(cc.Sprite).spriteFrame = this.blockSp.getSpriteFrame('block_blue')
                break;
            case 4:
                this.blockBg.getComponent(cc.Sprite).spriteFrame = this.blockSp.getSpriteFrame('block_yellow')
                break;
            case 5:
                this.blockBg.getComponent(cc.Sprite).spriteFrame = this.blockSp.getSpriteFrame('block_purple')
                break;
        }

        // 播放特效
        if (playEffect) {
            this.node.runAction(cc.sequence(
                cc.scaleTo(0.15, 0.9),
                cc.scaleTo(0.15, 1)
            ));
        }
    }
}
